package com.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加一些点(实际项目中从数据库查询出来
     */
    public void addMapPoint() {
        //添加测试数据
        BoundGeoOperations rt = redisTemplate.boundGeoOps("p");
        rt.add(new Point(118.803805, 32.060168), "南京");
        rt.add(new Point(116.397039, 39.9077), "北京");
        rt.add(new Point(113, 28.21), "长沙");
        rt.add(new Point(113.16, 27.83), "株洲");

        //求两点距离：单位km
        Distance distance = rt.distance("长沙", "株洲", Metrics.KILOMETERS);
        System.out.println(distance.getValue());

        //求附近的人:以株洲为中心，方圆50公里的人
        RedisGeoCommands.GeoRadiusCommandArgs geoRadiusArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
        geoRadiusArgs.includeDistance().includeCoordinates().sortAscending().limit(4);

        String zhuzhou = "株洲";
        GeoResults<RedisGeoCommands.GeoLocation> results = rt.radius(zhuzhou, new Distance(1500, Metrics.KILOMETERS),geoRadiusArgs);
        for (GeoResult<RedisGeoCommands.GeoLocation> r : results) {
            RedisGeoCommands.GeoLocation  location = r.getContent();
            String name = (String)location.getName(); //哪个人(地址名称)
            Distance d = r.getDistance();
            System.out.println(name+"\t"+d.getValue());


        }
    }


}
